package com.asia5b.a5mall.member.adapter

import androidx.core.content.ContextCompat
import android.widget.LinearLayout
import android.widget.TextView
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.member.databinding.ItemCouponlistAdapterBinding
import com.asia5b.a5mall.middleware.model.member.CouponModel
import com.asia5b.a5mall.middleware.utils.WTimeUtils
import com.asia5b.a5mall.middleware.utils.judge
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class CouponListAdapter(list: ArrayList<CouponModel>) :
        BaseQuickAdapter<CouponModel, BaseDataBindingHolder<ItemCouponlistAdapterBinding>>(R.layout.item_couponlist_adapter, list) {
    init {
        addChildClickViewIds(R.id.tv_couponlist_use_now)
        addChildClickViewIds(R.id.tv_couponlist_shopname)
    }
    override fun convert(helper: BaseDataBindingHolder<ItemCouponlistAdapterBinding>, item: CouponModel) {

        val binding = helper.dataBinding!!
        binding.model = item

        //设置背景
        val ll_couponlist_price_vessel=helper.getView<LinearLayout>(R.id.ll_couponlist_price_vessel)
        val priceBg=ContextCompat.getDrawable(context,(item.used_status==0).judge(R.drawable.ic_coupon_unuse,R.drawable.ic_coupon_useed))
        ll_couponlist_price_vessel.background=priceBg

        //优惠额度
        helper.setText(R.id.tv_couponlist_price, String.format("¥%.2f", item.coupon_price))

        //优惠要求额度
        helper.setText(R.id.tv_couponlist_price_min, String.format(ResString.get(R.string.满_数2_元可使用), String.format("%.2f",item.coupon_threshold_price)))

        //店铺性质
        helper.setText(R.id.tv_couponlist_selltype, (item.seller_id == 1).judge(ResString.get(R.string.自营), ResString.get(R.string.店铺)))
        val tv_couponlist_selltype=helper.getView<TextView>(R.id.tv_couponlist_selltype)
        //设置背景
        val sellBg=ContextCompat.getDrawable(context,(item.used_status==0).judge(R.drawable.rec1_t_blue_2dp,R.drawable.rec1_t_gray_2dp))
        tv_couponlist_selltype.background=sellBg

        //使用说明
        helper.setText(R.id.tv_couponlist_note, String.format(ResString.get(R.string.仅可购买_数_店铺商品), item.seller_name))

        //使用期限
        val startTime = WTimeUtils.format("${item.start_time * 1000}", WTimeUtils.text_yyyy_MM_dd)
        val endTime = WTimeUtils.format("${item.end_time * 1000}", WTimeUtils.text_yyyy_MM_dd)
        helper.setText(R.id.tv_couponlist_time, String.format("%s-%s", startTime, endTime))

        //立即使用
        helper.setGone(R.id.tv_couponlist_use_now,item.used_status!=0)

        //店铺名称
        helper.setText(R.id.tv_couponlist_shopname, item.seller_name)


        //是否已使用
        helper.setGone(R.id.tv_couponlist_usedicon,item.used_status==0)
    }
}